package com.micro.ai.files.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.files.entity.FileFavorite;
import com.micro.ai.files.service.FileFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 文件收藏控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/file-favorites")
@Tag(name = "文件收藏管理", description = "文件收藏的添加、取消、查询等功能")
public class FileFavoriteController {

    @Autowired
    private FileFavoriteService fileFavoriteService;

    @PostMapping
    @PreAuthorize("hasAuthority('file_favorite:create')")
    @Operation(summary = "添加收藏", description = "添加文件到收藏")
    public ApiResponse<FileFavorite> addFavorite(@Valid @RequestBody FileFavorite favorite) {
        FileFavorite result = fileFavoriteService.addFavorite(favorite);
        return ApiResponse.success("添加成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('file_favorite:delete')")
    @Operation(summary = "取消收藏", description = "根据收藏ID取消收藏")
    public ApiResponse<Void> removeFavorite(
            @Parameter(description = "收藏ID", required = true) @PathVariable String id) {
        fileFavoriteService.removeFavorite(id);
        return ApiResponse.successVoid("取消收藏成功");
    }

    @DeleteMapping("/by-user-file")
    @PreAuthorize("hasAuthority('file_favorite:delete')")
    @Operation(summary = "取消收藏", description = "根据用户和文件取消收藏")
    public ApiResponse<Void> removeFavoriteByUserAndFile(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") String userId,
            @Parameter(description = "文件ID", required = true) @RequestParam("fileId") String fileId) {
        fileFavoriteService.removeFavoriteByUserAndFile(tenantId, userId, fileId);
        return ApiResponse.successVoid("取消收藏成功");
    }

    @GetMapping("/check")
    @PreAuthorize("hasAuthority('file_favorite:view')")
    @Operation(summary = "检查是否已收藏", description = "检查文件是否已被用户收藏")
    public ApiResponse<Boolean> isFavorited(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") String userId,
            @Parameter(description = "文件ID", required = true) @RequestParam("fileId") String fileId) {
        boolean favorited = fileFavoriteService.isFavorited(tenantId, userId, fileId);
        return ApiResponse.success(favorited);
    }

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('file_favorite:view')")
    @Operation(summary = "分页查询收藏列表", description = "分页查询收藏列表")
    public ApiResponse<Page<FileFavorite>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") String userId) {
        Page<FileFavorite> page = fileFavoriteService.pageQuery(pageNum, pageSize, tenantId, userId);
        return ApiResponse.success(page);
    }

    @GetMapping("/by-user")
    @PreAuthorize("hasAuthority('file_favorite:view')")
    @Operation(summary = "查询用户的收藏列表", description = "查询用户的所有收藏")
    public ApiResponse<List<FileFavorite>> listByUser(
            @Parameter(description = "租户ID", required = true) @RequestParam("tenantId") String tenantId,
            @Parameter(description = "用户ID", required = true) @RequestParam("userId") String userId) {
        List<FileFavorite> list = fileFavoriteService.listByUser(tenantId, userId);
        return ApiResponse.success(list);
    }
}

